<template>
	<view class="follow">
		<!-- 导航-->
		<view class="follow-tab">
			<view class="follow-tab__box">
				<view class="follow-tab__item" :class="{active:activeIndex === 0 ? 'active' : ''}" @click="tab(0)">文章</view>
				<view class="follow-tab__item" :class="{active:activeIndex === 1 ? 'active' : ''}" @click="tab(1)">作者</view>
			</view>
		</view>
		<view class="follow-list">
			<swiper class="follow-list__item" :current="activeIndex" @change="change">
				<swiper-item>
					<view class="scroll">
						<scroll-view class="list-scroll" scroll-y enable-flex>
							<view>
								<uni-load-more v-if="list.length === 0 && !articleShow" iconType="snow" status="loading"></uni-load-more>
								<list-card mode="base" :item="item" v-for="item in list" :key="item._id" types="follow"></list-card>
								<view class="no-data" v-if="articleShow">
									没有收藏文章
								</view>
							</view>
						</scroll-view>
					</view>
				</swiper-item>
				<swiper-item>
					<view class="scroll">
						<scroll-view class="list-scroll" scroll-y enable-flex>
							<view>
								<uni-load-more v-if="list.length === 0 && !followShow" iconType="snow" status="loading"></uni-load-more>
								<list-author :item="item" v-for="item in authorList" :key="item.id"></list-author>
								<view class="no-data" v-if="followShow">
									没有关注作者
								</view>
							</view>
						</scroll-view>
					</view>
				</swiper-item>
			</swiper>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				activeIndex: 0,
				list: [],
				authorList: [],
				articleShow: false,
				followShow: false
			}
		},
		onLoad() {
			// 自定义事件 $on 只会在打开的页面触发
			uni.$on('update_article', () => {
				// console.log('关注页面触发');
				this.getFollow()
			})
			uni.$on('update_author', () => {
				this.getAuthor()
			})
			this.getFollow()
			this.getAuthor()
		},
		methods: {
			change(e) {
				this.activeIndex = e.detail.current
			},
			tab(index) {
				this.activeIndex = index
			},
			// 获取关注作品信息
			getFollow() {
				this.$api.get_follow().then(res => {
					const { data } = res
					this.list = data
					// console.log(res);
					this.articleShow = this.list.length === 0 ? true : false
				})
			},
			// 获取关注的作者
			getAuthor() {
				this.$api.get_author().then(res => {
					const { data } = res
					this.authorList = data
					this.followShow = this.list.length === 0 ? true : false
				})
			}
		}
	}
</script>

<style lang="scss">
	page {
		height: 100%;
		display: flex;
	}
	.follow {
		flex: 1;
		display: flex;
		flex-direction: column;
		box-sizing: border-box;
		.follow-tab {
			height: 30px;
			padding: 10px 20px;
			border-bottom: 1px #F5F5F5 solid;
			.follow-tab__box {
				width: 100%;
				height: 100%;
				display: flex;
				border: 1px $mk-basic-color solid;
				border-radius: 5px;
				.follow-tab__item {
					display: flex;
					width: 100%;
					justify-content: center;
					align-items: center;
					color: #666;
					font-size: 14px;
					&:first-child {
						border-right: 1px $mk-basic-color solid;
					}
					&.active {
						color: $mk-basic-color;
					}
				}
			}
		}
		.follow-list {
			height: 100%;
			flex: 1;
			.follow-list__item {
				height: 100%;
				.scroll {
					height: 100%;
					.list-scroll {
						height: 100%;
					}
				}
			}
		}
		.no-data {
			width: 100%;
			height: 200px;
			line-height: 200px;
			text-align: center;
			font-size: 14px;
			color: #999;
		}
	}
</style>
